﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using System.Data;
namespace QUANLIBANHANG.GUI
{
    public class WordUltil
    {
        Application app;
        Document doc;
        private object path;
        public WordUltil(string vpath, bool creatapp)
        {
            path = vpath;
            app = new Application();
            app.Visible = creatapp;
            object ob = System.Reflection.Missing.Value;
            doc = app.Documents.Add(ref path, ref ob, ref ob, ref ob);
        }
        // Ghi dử liệu vào file word
        public void writeFields(Dictionary<string, string> value)
        {
            foreach (Field field in doc.Fields)
            {
                string fieldname = field.Code.Text.Substring(11, field.Code.Text.IndexOf("\\") - 12).Trim();
                if (value.ContainsKey(fieldname))
                {
                    field.Select();
                    app.Selection.TypeText(value[fieldname]);
                }
            }
        }
        // Ghi du liêu lên Table trong word
        public void writeTable(System.Data.DataTable dt, int indextable)
        {
            Table tb = doc.Tables[indextable];
            int lenrow = dt.Rows.Count;
            int lencol = dt.Columns.Count;
            for (int i = 0; i < lenrow; ++i)
            {
                object ob = System.Reflection.Missing.Value;
                tb.Rows.Add(ref ob);
                for (int j = 0; j < lencol; ++j)
                {
                    tb.Cell(i + 2, j + 1).Range.Text = dt.Rows[i][j].ToString();
                }
            }


        }
    }
}
